package com.xdu.math.Service;

import com.xdu.math.Dao.UserMapper;
import com.xdu.math.Entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{
        UserInfo userInfo =  userMapper.getUserInfoByEmail(username);
        if(userInfo == null){
            throw new UsernameNotFoundException("邮箱不存在：" + username);
        }else{
            return User.builder()
                    .username(userInfo.getUserEmail())
                    .password(userInfo.getPassword())
                    .roles("USER")
                    .build();
        }
    }

}
